package com.xhrd.framework.web.taglib;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.xhrd.framework.vo.Page;

/**
 * 分页显示
 *
 * @author bobby
 *
 */
public class PageTag extends TagSupport {

    /**
     * 序列化ID
     */
    private static final long serialVersionUID = 4902903427014161397L;

    /**
     * 样式名
     */
    private String cssClass;

    /**
     * 页号元素的ID
     */
    private String pageNumberElementId;

    /**
     * 查询form的ID
     */
    private String searchFormId;

    /**
     * 分页对象名
     */
    private String pageObject;

    /**
     * 分页对象上下文
     */
    private String contextType;

    /**
     * 页号个数(2|3|4)
     */
    private int rangeRadius = 2;

    /**
     * 上下文类型-request
     */
    private static final String CONTEXT_TYPE_REQUEST = "0";

    /**
     * 上下文类型session
     */
    private static final String CONTEXT_TYPE_SESSION = "1";

    /**
     * @return the cssClass
     */
    public String getCssClass() {
        return cssClass;
    }

    /**
     * @param cssClass
     *            the cssClass to set
     */
    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    /**
     * @return the pageObject
     */
    public String getPageObject() {
        return pageObject;
    }

    /**
     * @param pageObject
     *            the pageObject to set
     */
    public void setPageObject(String pageObject) {
        this.pageObject = pageObject;
    }

    /**
     * @return the contextType
     */
    public String getContextType() {
        return contextType;
    }

    /**
     * @param contextType
     *            the contextType to set
     */
    public void setContextType(String contextType) {
        this.contextType = contextType;
    }

    public int getRangeRadius() {
        return rangeRadius;
    }

    public void setRangeRadius(int rangeRadius) {
        this.rangeRadius = rangeRadius;
    }

    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        // 初始化基本参数
        if (StringUtils.isEmpty(searchFormId)) {
            searchFormId = "searchForm";
        }
        if (StringUtils.isEmpty(pageNumberElementId)) {
            pageNumberElementId = "pageNumber";
        }
        if (StringUtils.isEmpty(cssClass)) {
            cssClass = "pagingStyle";
        }
        if (StringUtils.isEmpty(contextType)) {
            contextType = CONTEXT_TYPE_REQUEST;
        }
        // 取得page对象存储的属性名
        if (StringUtils.isEmpty(pageObject)) {
            pageObject = "pageObject";
        }
        // 取得page对象
        Page<?> page = null;
        if (CONTEXT_TYPE_REQUEST.equals(contextType)) {
            page = (Page<?>) request.getAttribute(pageObject);
        } else if (CONTEXT_TYPE_SESSION.equals(contextType)) {
            page = (Page<?>) request.getSession().getAttribute(pageObject);
        } else {
            return super.doStartTag();
        }
        try {
            // 构造script
            String scriptHtml = buildPageChangeScriptHtml();
            if (page.getLastPageNumber() < 1) {
                return super.doStartTag();
            }
            // 构造分页导航信息
            String pagingHtml = buildPagingHtml(page);
            out.write(scriptHtml);
            out.write(pagingHtml);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.doStartTag();
    }

    /**
     * 构造分页用脚本代码
     *
     * @return
     */
    private String buildPageChangeScriptHtml() {
        StringBuilder scriptHtml = new StringBuilder();
        // 单击链接时调用的函数
        scriptHtml.append("<script language='javascript'>");
        scriptHtml.append("	function submitPageSearch_" + searchFormId + "(targetPageNumber) {");
        scriptHtml.append("    	$('#" + pageNumberElementId + "').val(targetPageNumber);");
        scriptHtml.append("    	$('#" + searchFormId + "').submit();");
        scriptHtml.append("	}");
        // 直接输入选择页号时调用的函数
        scriptHtml.append("	function selectPageSubmit_" + searchFormId + "() {");
        scriptHtml.append("		var selectedPage = $('#selectedPage_" + searchFormId + "').val();");
        scriptHtml.append("		if('' != $.trim(selectedPage)) {");
        scriptHtml.append("			$('#" + pageNumberElementId + "').val(selectedPage-1);}");
        scriptHtml.append("			$('#" + searchFormId + "').submit();");
        scriptHtml.append("	}");
        scriptHtml.append("</script>");
        return scriptHtml.toString();
    }

    /**
     * 构造分页导航信息
     *
     * @param page
     *            分页对象
     * @return
     */
    private String buildPagingHtml(Page<?> page) {
        StringBuilder pagingHtml = new StringBuilder();
        int targetPageNumber;
        // 构造分页信息
        pagingHtml.append("<nav><ul class='" + cssClass + "' >");

        if (page.isFirstPage()) {
            pagingHtml.append("<li class='disabled'><a href='#'>&laquo;</a></li>");
        } else {
            targetPageNumber = 0;
            pagingHtml.append("<li><a href='javascript:void(0)' onclick='submitPageSearch_" + searchFormId + "(" + targetPageNumber
                    + ")'>&laquo; </a></li>");
        }

        int startIndex = page.getThisPageNumber() - getRangeRadius() > 0 ? page.getThisPageNumber() - getRangeRadius() : 0;
        int endIndex = page.getThisPageNumber() + getRangeRadius() > page.getLastPageNumber() ? page.getLastPageNumber() : page.getThisPageNumber()
                + getRangeRadius();
        for (int i = startIndex; i <= endIndex && i >= startIndex; i++) {
            targetPageNumber = i + 1;
            if (i == page.getThisPageNumber()) {
                pagingHtml.append("<li class='active'><a href='javascript:void(0)' onclick='submitPageSearch_" + searchFormId + "(" + i + ")'>"
                        + targetPageNumber + "</a></li>");
            } else {
                pagingHtml.append("<li><a href='javascript:void(0)' onclick='submitPageSearch_" + searchFormId + "(" + i + ")'>" + targetPageNumber
                        + "</a></li>");
            }
        }

        if (page.isLastPage()) {
            pagingHtml.append("<li class='disabled'><a href='#'>&raquo;</a></li>");
        } else {
            targetPageNumber = page.getLastPageNumber();
            pagingHtml.append("<li><a href='javascript:void(0)' onclick='submitPageSearch_" + searchFormId + "(" + targetPageNumber
                    + ")'>&raquo;</a></li>");
        }

        pagingHtml.append("</ul></nav>");
        return pagingHtml.toString();
    }
}